﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Geometry.Core;
using Geometry.Core.Business;
using Geometry.Core.Services;
using Geometry.Service;
using Geometry.Core.Models;

namespace Geometry.Web.Businesses
{
    public class TriangleBusiness : ITriangleBusiness
    {
        private readonly ITriangleService triangleService;

        public TriangleBusiness()
        {
            this.triangleService = new TriangleService();
        }

        public bool IsTriangle(int side1, int side2, int side3)
        {
            return triangleService.IsTriangle(side1, side2, side3);
        }
        
        public Triangle CheckExistTriangle(Triangle model)
        {
            return triangleService.CheckExistTriangle(model);
        }

        public Triangle CalculateNewTriangle(Triangle model)
        {
           
            int side1 = model.Side1.Value;
            int side2 = model.Side2.Value;
            int side3 = model.Side3.Value;

            if (!IsTriangle(side1, side2, side3) ||
              !IsTriangle(side2, side1, side3) ||
              !IsTriangle(side3, side1, side2))
            {
                model.IsTriangle = false;

            }
            else
            {
                model.IsTriangle = true;
                //Equilateral
                if ((side1 == side2) && (side1 == side3))
                {
                    model.TriangleType = "Equilateral";
                }
                //Isosceles
                else if ((side1 == side2) || (side2 == side3) || (side1 == side3))
                {
                    model.TriangleType = "Isosceles";
                }
                //Square 
                else if(((side1*side1)==((side2*side2) + (side3*side3)))||
                    ((side2*side2)==((side1*side1) + (side3*side3)))||
                    ((side3*side3)==((side2*side2) + (side1*side1))))
                {
                    model.TriangleType = "Square";
                }
                else
                {
                    model.TriangleType = "Normal";
                }
            }

            triangleService.Insert(model);
            return model;
        }


        public List<TriangleResult> StatisticResult()
        {
            return triangleService.GetResult();
        }


        public bool Validation(Triangle model)
        {
            return triangleService.Validation(model);
        }
    }
}